package com.ktjiaoyu.controller;

import com.ktjiaoyu.pojo.Video;
import com.ktjiaoyu.service.VideoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VideoController {
    @Resource
    VideoService videoService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    @RequestMapping("/edit")
    public String edit(Long id){
        return "edit";
    }

    /**
     * 分页查询
     * @param category
     * @param languageId
     * @param pageIndex
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map<String,Object> list(@RequestParam(required = false) String category,
                                   @RequestParam(required = false) Long languageId,
                                   @RequestParam(required = false,defaultValue = "1") Integer pageIndex){
        //每页显示多少条数据
        Integer pageSize=3;

        //首先查询总记录数
        Integer totalCount = videoService.selectCount(category, languageId);
        //计算总页数
        Integer totalPage=0;
        if(totalCount%pageSize==0){
            totalPage=totalCount/pageSize;
        }else{
            totalPage=totalCount/pageSize+1;
        }
        //查询数据
        List<Video> list = videoService.selectPage(category, languageId, pageIndex, pageSize);

        Map<String,Object> map=new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);
        map.put("list",list);

        return map;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object> save(Video video){
        Map<String,Object> map=new HashMap<>();

        int rs = videoService.insert(video);
        if(rs>0){
            map.put("result",true);
        }else{
            map.put("result",false);
        }

        return map;
    }

    @RequestMapping("/getVideo")
    @ResponseBody
    public Map<String,Object> getVideo(Long id){
        Map<String,Object> map=new HashMap<>();

        Video video = videoService.selectOne(id);
        map.put("video",video);
        return map;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(Video video){
        Map<String,Object> map=new HashMap<>();

        int rs = videoService.update(video);
        if(rs>0){
            map.put("result",true);
        }else{
            map.put("result",false);
        }

        return map;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> map=new HashMap<>();

        int rs = videoService.delete(id);
        if(rs>0){
            map.put("result",true);
        }else{
            map.put("result",false);
        }

        return map;
    }

}
